﻿function onActivityDrop(e) {
    var $target = $(e.dropTarget);
    var treeview = $('#activities').data('tTreeView');
    var nodeText = treeview.getItemText(e.item);

    if ($target.is("ul.ws-activities")) {
        e.preventDefault();
        $newli = $target.addListItem(null);
         var nodetext = getTreeviewSelectedItemText($('#conditions'),e);
         initialActivityLi($newli, nodetext);
    }
    return;
}
function initialActivityLi($newli, nodetext) {
    var $target = $('.ws-activitis');
    $newli.val(nodetext).find('.display-text').text(nodetext);
    $newli.addHoverFunctions({
        deleteEvent: function () {
            $newli.remove();
        },
        detailEvent: function () {
            setDetailToWindowEditor($newli, initialActivityDetail($newli), $('#Window'));
        }
    });
}

function initialActivityDetail($target) {
    var data = $target.data('lidata');
    var array = new Array();
    array.push({ controltype: 'text', fieldname: '环节名称', fieldvalue: data == null ? null : data.ActivityName, must: true });
    array.push({ controltype: 'text', fieldname: '环节时间', fieldvalue: data == null ? null : data.ActivityDuration, must: true });
    array.push({ controltype: 'peoplefinder', fieldname: '环节角色', fieldvalue: data == null ? null : data.ActivityRole, must: true });
    array.push({ controltype: 'radio', fieldname: '环节类型', fieldvalue: data == null ? null : data.ActivityType, optionalvalue: '串签||并签' });
    array.push({ controltype: 'slide', fieldname: '环节级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    $target.data('av', array);

    var win = $('#Window').data('tWindow');
    var $table = $target.getTable(
                         {
                             save: function () {
                                 if ($target.attr('sucess') == 'true') {
                                     $target.find('.text-display').text(array[0].fieldvalue);
                                     $target.removeClass('unsave').addClass('saved');
                                     saveActivityData($target);
                                     win.close();
                                 }
                             },
                             cancel: function () {
                                 win.close();
                             }
                         });
    return $table;
}

function saveActivityData($li) {
    var av = $li.data('av');
    var lidata = { ActivityName: av[0].fieldvalue, ActivityDuration: av[1].fieldvalue, ActivityRole: av[2].fieldvalue, ActivityType: av[3].fieldvalue, Level: av[4].fieldvalue }
    $li.data('lidata', lidata);
}
